package com.example.restatr.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.restatr.Bean.CardBean;
import com.example.restatr.Bean.ServiceBean;
import com.example.restatr.R;
import com.example.restatr.Tools;

import java.util.ArrayList;
import java.util.List;

public class CardAdapter extends BaseAdapter {
    private String type;
    private Context context;
    private List<CardBean> cardBeans;

    public CardAdapter(String type, Context context, List<CardBean> cardBeans) {
        this.type = type;
        this.context = context;
        this.cardBeans = cardBeans;
    }

    @Override
    public int getCount() {
        return cardBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return cardBeans.get( position );
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from( context ).inflate( R.layout.card_item_layout, null );
        ImageView icon = convertView.findViewById( R.id.icon );
        TextView text = convertView.findViewById( R.id.text );

        switch (type) {
            case "url":
                SetIcon( icon, position, R.drawable.ic_gd, convertView, text );
                break;
            case "wm":
                SetIcon( icon, position, R.drawable.ic_round_star_24, convertView, text );
                break;
            case "id":
                Glide.with( context )
                        .load( Integer.valueOf( cardBeans.get( position ).getUrl() ) )
                        .transform(new CircleCrop())
                        .placeholder( R.drawable.bj )
                        .error( R.drawable.ic_gd )
                        .into( icon );
                text.setText( cardBeans.get( position ).getTitle() );
                convertView.setTag( cardBeans.get( position ).getTitle() );
                break;
        }

        return convertView;
    }

    private void SetIcon(ImageView icon, int position, int errorDrawable, View view, TextView textView) {
        Glide.with( context )
                .load( Tools.ip + cardBeans.get( position ).getUrl() )
                .transform(new CircleCrop())
                .placeholder( R.drawable.bj )
                .error( errorDrawable )
                .into( icon );
        textView.setText( cardBeans.get( position ).getTitle() );
        view.setTag( cardBeans.get( position ).getTitle() );

    }
}
